import java.util.Date;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-22
 * Time: 16:03
 */
//实现代码, 能够创建两个线程, 对这个数组的所有元素求和.
//记录程序的执行时间.
public class Test {
    public static int[] arr=new int[100_0000];
    public static long sum1=0;
    public static long sum2=0;
    public static void main(String[] args) throws InterruptedException{
        Random random=new Random();
        for(int i=0;i<arr.length;i++){
            arr[i]=random.nextInt(100)+1;
        }
        Thread t1=new Thread(){
            @Override
            public void run() {
                for(int i=0;i<100_0000;i+=2) {
                    sum1 += arr[i];
                }
            }
        };
        Thread t2=new Thread(){
            @Override
            public void run() {
                for(int i=1;i<100_0000;i+=2){
                    sum2+=arr[i];
                }
            }
        };
        Date time1=new Date();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long sum=sum1+sum2;
        System.out.println(sum);
        Date time2=new Date();
        System.out.println("运行时间："+(time2.getTime()-time1.getTime())+"毫秒");
    }
}
